package com.beijiteshop.libcommon.utils

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/5 17:22
 * @Description 事件通信
 */
object EventUtil {

    /**
     * 发送消息
     */
    fun post(key: String,value: Any? = null){
        LiveEventBus
            .get(key)
            .post(value)
    }

    /**
     * 订阅消息
     */
    inline fun <reified T: Any?> observe(lifecycleOwner: LifecycleOwner,key: String,crossinline onChange:(value: T) -> Unit){
        LiveEventBus.get(key,T::class.java).observe(lifecycleOwner, Observer { value->
            onChange.invoke(value)
        })
    }
}
